"""配置管理模块"""
import json
import os
from typing import Dict, Any

class ConfigManager:
    """配置管理类"""
    
    def __init__(self, config_file: str = 'config.py'):
        """初始化配置管理器
        
        Args:
            config_file (str): 配置文件路径
        """
        self.config_file = config_file
        self.config: Dict[str, Any] = {}
        
    def load_config(self) -> Dict[str, Any]:
        """加载配置文件
        
        Returns:
            Dict[str, Any]: 配置字典
        """
        try:
            if os.path.exists(self.config_file):
                with open(self.config_file, 'r', encoding='utf-8') as f:
                    content = f.read()
                    # 提取config字典
                    exec(content, globals())
                    self.config = globals()['config']
            return self.config
        except Exception as e:
            raise Exception(f"加载配置文件失败：{str(e)}")
            
    def save_config(self, config: Dict[str, Any]) -> None:
        """保存配置到文件
        
        Args:
            config (Dict[str, Any]): 配置字典
        """
        try:
            self.config = config
            # 生成配置文件内容
            config_content = f"config = {json.dumps(config, ensure_ascii=False, indent=4)}"
            
            # 保存到文件
            with open(self.config_file, 'w', encoding='utf-8') as f:
                f.write(config_content)
        except Exception as e:
            raise Exception(f"保存配置失败：{str(e)}")
            
    def get_config(self) -> Dict[str, Any]:
        """获取当前配置
        
        Returns:
            Dict[str, Any]: 配置字典
        """
        return self.config 